﻿using HalconDotNet;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZRFCore.Camera;
using MultiAxisAssembly;
using System.Threading;

namespace 喷点胶机
{
    public partial class Page_1_SnapImg : UIPage
    {
        private ControlMaster controlMaster;
        private ICamera camera;
        public Page_1_SnapImg(ControlMaster controlMaster)
        {
            InitializeComponent();
            this.zrf_hWindow1.Location = new System.Drawing.Point(12, 12);
            this.zrf_hWindow1.Size = new System.Drawing.Size(1167, 809);
            this.controlMaster = controlMaster;
            this.camera = controlMaster.Camera;
            this.KeyDown += controlMaster.MulitAxisMasterControl.KeyDown;
            this.KeyUp += controlMaster.MulitAxisMasterControl.KeyUp;
        }


        private void btPlay_Click(object sender, EventArgs e)
        {
            camera.TrigSource = ZRFCore.Camera.TrigSource.Continuous;
            camera.StartGrabbing();
        }

        private void btCapture_Click(object sender, EventArgs e)
        {
            camera.StopGrabbing();
            if (zrf_hWindow1.Image != null && zrf_hWindow1.Image.IsInitialized())
            {
                controlMaster.StandardImg = zrf_hWindow1.Image.Clone();
                zrf_hWindow1.DispImage(controlMaster.StandardImg);
            }
            else
            {
                ShowWarningDialog("窗口图像为空，请先打开播放");
            }
        }

        public override void Init()
        {
            base.Init();
            try
            {
                camera.ImageCallbackEvent += Camera_ImageCallbackEvent;
                if (controlMaster.StandardImg != null && controlMaster.StandardImg.IsInitialized())
                {
                    zrf_hWindow1.DispImage(controlMaster.StandardImg);
                }
                else
                {
                    camera.TrigSource = ZRFCore.Camera.TrigSource.Continuous;
                    camera.StartGrabbing();
                }
            }
            catch (Exception e)
            {
                ShowErrorDialog(e.Message);
            }
            Task.Run(() => 
            {
                Thread.Sleep(500);
                this.Invoke(new Action(() =>
                {
                    this.Focus();//设置焦点
                }));
            });
        }

        public override void Final()
        {
            base.Final();
            try
            {
                camera.ImageCallbackEvent -= Camera_ImageCallbackEvent;
                camera.StopGrabbing();
            }
            catch (Exception e)
            {
                ShowErrorDialog(e.Message);
            }
           
        }

        private void btSetting_Click(object sender, EventArgs e)
        {
            camera.OpenSettingForm();
        }

        private void Camera_ImageCallbackEvent(ZRFCore.Camera.ICamera sender, ZRFCore.Camera.ImageCallbackEventArgs e)
        {
            try
            {
                zrf_hWindow1.DispImage(e.HImage);
            }
            catch (Exception ee)
            {
                ShowErrorDialog(ee.Message);
            }
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            Task.Run(() =>
            {
                controlMaster.GotoCalibPos();
                camera.StopGrabbing();
                if (zrf_hWindow1.Image != null && zrf_hWindow1.Image.IsInitialized())
                {
                    controlMaster.StandardImg = zrf_hWindow1.Image.Clone();
                    zrf_hWindow1.DispImage(controlMaster.StandardImg);
                }
                else
                {
                    ShowWarningDialog("窗口图像为空，请先打开播放");
                }
            });
        }
    }
   
}
